$(function () {
    obj.init();
});

var obj = new function() {
    return {
        form: null,
        id: null,
        init: function () {
            this.id = xhTool.getQuery('id');
            this.form = new xhForm($('#modify_charge_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addProjectData();
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#project').on('change',function () {
                //获取金额填入
                $this.funObj().getMoney();
            });
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addProjectData: function () {
                    var proArr = [];
                    projectObj = $('#project').select2({placeholder: '选择项目',data: projects.project,allowClear: true,multiple : true});//填充所有options
                    $.each(project_data, function (key, value) {
                        proArr.push(value);
                    });
                    projectObj.val(proArr).trigger('change');//填充已存在数据
                },
                getMoney: function () {
                    var project = $('#project').val();
                    xhExtra.ajaxRequest('device/getChargeData', {project:project, model:$('#model').val()},
                        {success: false,check:false}, function (data) {
                            $('#money').val(data);
                        });
                },
                saveData: function () {
                    var id = $this.id;
                    var project = $('#project').val();
                    if (project < 1 || project.length < 1){
                        window.top.xhTop.showError('请选择维修项目');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    xhExtra.ajaxRequest('Device/modifyChargeData', {id:id, project:project, money:$('#money').val()},
                        {success: true,check:false},function (res) {
                            if(res.code === 200){
                                window.top.swal('成功',res.msg,'success');
                                window.top.xhTop.closeModal();
                                window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            } else {
                                window.top.xhTop.showError(res.error);
                                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            }
                        });
                }
            }
        }
    }
};
